package com.example.content.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

/**
 * Created by zhudongao
 * Desc
 * Date 2020/9/29
 */
abstract class BaseLazyFragment:Fragment() {
    private var isViewCreated = false
    private var isInitialized = false

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            prepareLoad()
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        isViewCreated = true
        prepareLoad()
    }

    private fun prepareLoad() {
        if (isViewCreated && !isInitialized) {
            onLazyLoad()
            isInitialized = true
        }
    }

    abstract fun onLazyLoad()
}